package com.fairytask.objects
{
	import com.fairytask.layer.BattleLayer;
	

	public class Player extends Object
	{
		protected var lastTime:uint = 0;
		public var numCell:uint = 1;
		public var numUnits:uint = 100;
		protected var _game:BattleLayer;
		public var isAI:Boolean = false;
		public var cells:Array;
		protected var changeTime:uint = 0;
		public var maxTime:uint = 10000;
		public var unitType:int = -1;
		protected var curTime:uint = 0;
		public var minTime:uint = 3000;
		
		public function Player()
		{
			_game = BattleLayer.instance;
			changeTime = randNum(minTime, maxTime);
		}
		
		public function hasLost() : Boolean
		{
			return numUnits <= 10 && numCell <= 0;
		}
		
		private function randNum(param1:Number, param2:Number) : Number
		{
			return param1 + Math.random() * (param2 - param1);
		}
		
		public function update(param1:int, param2:Number) : void
		{
			var _loc_3:* = 0;
			var _loc_4:* = 0;
			var _loc_5:* = null;
			var _loc_6:* = null;
			var _loc_7:* = null;
			var _loc_8:* = 0;
			var _loc_9:* = 0;
			var _loc_10:* = 0;
			if (!isAI || !cells || cells.length <= 0 || numCell <= 0 || !_game ||  _game.diff > _game.maxDiff)
			{
				return;
			}
			curTime = param1 - lastTime;
			if (curTime > changeTime)
			{
				_loc_3 = _game.diff;
				_loc_4 = _game.maxDiff * 1.5;
				_loc_5 = _game.allPlayers;
				_loc_6 = cells[Math.floor(randNum(0, cells.length))];
				while (_loc_6.unitType != unitType)
				{
					
					_loc_6 = cells[Math.floor(randNum(0, cells.length))];
				}
				_loc_7 = cells[Math.floor(randNum(0, cells.length))];
				while (_loc_7 == _loc_6)
				{
					
					_loc_7 = cells[Math.floor(randNum(0, cells.length))];
				}
				_loc_8 = Math.round(Math.random() * _loc_4);
				if (_loc_8 <= _loc_3 || numCell > cells.length / 3)
				{
					_loc_9 = Math.floor(Math.random() * _loc_5.length);
					while (_loc_5[_loc_9].unitType == unitType)
					{
						
						_loc_9 = Math.floor(Math.random() * _loc_5.length);
					}
					if (_loc_5[_loc_9].numCell > 0)
					{
						while (_loc_7.unitType != _loc_5[_loc_9].unitType)
						{
							
							_loc_7 = cells[Math.floor(randNum(0, cells.length))];
						}
					}
				}
				if (_loc_6.numUnits > 10 + _loc_3 / _loc_4 * 20)
				{
					_loc_10 = Math.round(_loc_6.numUnits / 2);
					_game.createViruses(_loc_6, _loc_7, _loc_10);
					_loc_6.numUnits = _loc_6.numUnits - _loc_10;
					changeTime = randNum(minTime * (1 - _loc_3 / _loc_4), maxTime * (1 - _loc_3 / _loc_4));
					lastTime = param1;
				}
			}
		}
		
	}
}